//
//  ZYComposeBottomView.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/17.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit

//底部按钮类型 枚举
enum childButtonType:Int {
    //图片
    case Picture = 0
    // @
    case Mention = 1
    // #
    case Trend = 2
    // 笑脸
    case Emoticon = 3
    // +
    case Add = 4
}


class ZYComposeBottomView: UIView {
    var emoticonButton:UIButton?
    var isEmoticon:Bool = false {
        didSet{
            let imgName:String
            if isEmoticon {
                imgName = "compose_keyboardbutton_background"
            }else{
                imgName = "compose_emoticonbutton_background"
            }
            emoticonButton?.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        let pictureButton = addChildButton("compose_camerabutton_background", type: .Picture)
        let mentionButton = addChildButton("compose_mentionbutton_background", type: .Mention)
        let trendButton = addChildButton("compose_trendbutton_background", type: .Trend)
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        let addButton = addChildButton("compose_add_background", type: .Add)
    
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
    
    }

    func addChildButton(imgName:String,type:childButtonType) -> UIButton{
        let btn = UIButton()
        btn.tag = type.rawValue
        btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        addSubview(btn)
        return btn
        
    }
    func buttonClick(btn:UIButton) {
//        printLog(btn.tag)//2
//        printLog(childButtonType(rawValue: btn.tag)!)//Trend
        closureOfButton?(type: childButtonType(rawValue: btn.tag)!)//?缺了，会导致没后面的参数提示
    }
    var closureOfButton:((type:childButtonType)->())?
}
